import { requireUser } from "#server/utils/context"; import { getProviderById } from "#server/service/llm"; export default defineWrappedResponseHandler(async (event) => { const user = await requireUser(event); const id = Number(getRouterParam(event, "id")); if (!id) { return R.throwError(400, "无效的供应商 ID", null); } const provider = await getProviderById(id, user.id); if (!provider) { return R.throwError(404, "供应商不存在", null); } const baseUrl = provider.baseUrl?.replace(/\/+$/, "") || ""; if (!baseUrl) { return R.success([]); } const headers: Record = { "Content-Type": "application/json", }; if (provider.parseMode === "anthropic") { if (provider.apiKey) { headers["x-api-key"] = provider.apiKey; } headers["anthropic-version"] = "2023-06-01"; } else { if (provider.apiKey) { headers["Authorization"] = `Bearer ${provider.apiKey}`; } } try { const res = await $fetch<{ data?: { id: string; name?: string }[] }>( `${baseUrl}/models`, { headers, timeout: 8000 }, ); const models = (res?.data ?? []).map((m) => ({ id: m.id, name: m.name || m.id, })); return R.success(models); } catch { return R.success([]); } });